package com.lianyu507.service.impl;

import com.lianyu507.bean.YmlConfig;
import com.lianyu507.mapper.NetworkExcelMapper;
import com.lianyu507.model.ArticleExcel;
import com.lianyu507.model.ExcelBean;
import com.lianyu507.model.MbUserExcel;
import com.lianyu507.model.ProfessionLogExcel;
import com.lianyu507.service.ExcelService;
import com.lianyu507.util.ExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;

@Service
public class ExcelServiceImpl implements ExcelService {

    /**
     *
     */
    @Autowired
    private YmlConfig config;
    @Autowired
    private NetworkExcelMapper networkExcelMapper;
    @Override
    @Scheduled(cron="0 0 1 * * ?")
    public void createArticleExcelFile() {
XSSFWorkbook xssfWorkbook = null;
try{
    List<ArticleExcel> lists = networkExcelMapper.getTitleAndCountByLogId(1);
    List<ExcelBean> excel=new ArrayList<>();
    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
    excel.add( new ExcelBean("文章题目","title",0));
    excel.add( new ExcelBean("点击次数","amount",0));
    map.put(0,excel);
    String  sheetName="文章点击量统计";
    xssfWorkbook= ExcelUtil.createExcelFile(ArticleExcel.class,lists,map,sheetName);
    OutputStream out=new FileOutputStream(config.getExportload()+ "文章点击量"+UUID.randomUUID() +".xls");
    xssfWorkbook.write(out);

}catch (Exception e){
e.printStackTrace();
}
    }

    @Scheduled(cron="0 0 1 * * ?")
    public void createUserNewWordsExcelFile(){
        XSSFWorkbook xssfWorkbook = null;
        try{
            List<MbUserExcel> lists = networkExcelMapper.getUserNewWords(2);
            List<ExcelBean> excel=new ArrayList<>();
            Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
            excel.add( new ExcelBean("用户","userName",0));
            excel.add( new ExcelBean("注册邮箱","userEmail",0));
            excel.add( new ExcelBean("生词量","amount",0));
            excel.add( new ExcelBean("日期","updateTime",0));
            map.put(0,excel);
            String  sheetName="会员生词量统计";
            xssfWorkbook= ExcelUtil.createExcelFile(MbUserExcel.class,lists,map,sheetName);
            OutputStream out=new FileOutputStream(config.getExportload()+ "会员生词量统计"+UUID.randomUUID() +".xls");
            xssfWorkbook.write(out);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Scheduled(cron="0 0 1 * * ?")
    public void createWordDaysExcelFile(){
        XSSFWorkbook xssfWorkbook = null;
        try{
            List<ProfessionLogExcel> lists = networkExcelMapper.getWordDays(3);
            List<ExcelBean> excel=new ArrayList<>();
            Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
            excel.add( new ExcelBean("日期","updateTime",0));
            excel.add( new ExcelBean("单词量","amount",0));
            map.put(0,excel);
            String  sheetName="每天爬虫单词量统计";
            xssfWorkbook= ExcelUtil.createExcelFile(ProfessionLogExcel.class,lists,map,sheetName);
            OutputStream out=new FileOutputStream(config.getExportload()+ "单词量统计"+UUID.randomUUID() +".xls");
            xssfWorkbook.write(out);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
